package com.yanqu.road.dao.impl.activity.doublespring;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.doublespring.data.DoubleSpringZlUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class DoubleSpringZlUserDataDaoImpl extends TempDataObjectDao<DoubleSpringZlUserData> {

    public static String selectSql = "select * from t_u_double_spring_zl_user_data where activityId = ? and userId = ?";
    public DoubleSpringZlUserData getDoubleSpringZlUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(selectSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String insertSql = "INSERT INTO `t_u_double_spring_zl_user_data`(`activityId`, `userId`, `exp`, `bysfMallId`, `qbjjMallId`, `createTime`) VALUES (?, ?, ?, ?, ?, ?)";
    @Override
    protected boolean addData(DoubleSpringZlUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getExp()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBysfMallId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getQbjjMallId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCreateTime()));
        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateSql = "UPDATE `t_u_double_spring_zl_user_data` SET `exp` = ?, `bysfMallId` = ?, `qbjjMallId` = ? WHERE `activityId` = ? AND `userId` = ? ";
    @Override
    protected boolean updateData(DoubleSpringZlUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getExp()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBysfMallId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getQbjjMallId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected DoubleSpringZlUserData parseData(ResultSet rs) throws SQLException {
        DoubleSpringZlUserData data = new DoubleSpringZlUserData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setExp(rs.getLong("exp"));
        data.setBysfMallId(rs.getInt("bysfMallId"));
        data.setQbjjMallId(rs.getInt("qbjjMallId"));
        data.setCreateTime(rs.getLong("createTime"));
        return data;
    }
}
